package org.example.spring.carsalemanagementsystem.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.spring.carsalemanagementsystem.dao.ClientDao;
import org.example.spring.carsalemanagementsystem.entity.Client;
import org.example.spring.carsalemanagementsystem.service.ClientService;
import org.example.spring.carsalemanagementsystem.utils.IDUtil;
import org.example.spring.carsalemanagementsystem.utils.Page;
import org.example.spring.carsalemanagementsystem.vo.ClientFuzzyVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ClientServiceImpl implements ClientService {
    private final ClientDao clientDao;

    @Override
    public void deleteById(String clientid) {
        clientDao.deleteById(clientid);
    }

    @Override
    public void insertOne(Client client) {
        client.setClientid(IDUtil.getId());
        client.setCreatetime(LocalDateTime.now());
        //默认启用
        client.setStatus(1);
        clientDao.insertOne(client);
    }

    @Override
    public void updateById(Client client) {
        clientDao.updateById(client);
    }

    @Override
    public Page<Client> findPages(Long pageNum, Long pageSize) {
        Page<Client> page=new Page<>();
        Long total= clientDao.count();
        List<Client> clients = clientDao.findPages(pageNum, pageSize);
        page.setTotal(total);
        page.setRows(clients);
        return page;
    }

    @Override
    public Page<Client> findPagesByKey(ClientFuzzyVo vo) {
        Page<Client> page=new Page<>();
        List<Client> clients = clientDao.findPagesByKey(vo);
        Long total = clientDao.countByKey(vo);
        page.setTotal(total);
        page.setRows(clients);
        return page;
    }

    @Override
    public void changeStatus(String clientid) {
        Client client = clientDao.selectById(clientid);
        Integer status = client.getStatus();
        if (status==0){
            clientDao.changeStatus(clientid,1);
        }else {
            clientDao.changeStatus(clientid,0);
        }
    }

    @Override
    public List<Client> findList() {
        return clientDao.findList();
    }
}
